package com.itheima.health.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    //新建套餐提交
    @Override
    public Boolean saveSetmealInfo(SetmealDTO setmealDTO) {

        //存储套餐表
        boolean flag = save(setmealDTO);
        //mp获取套餐主键
        Integer gid = setmealDTO.getId();
        //获取检查组集合
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        //判断是否有检查组
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            //遍历放入中间表t_setmeal_checkgroup
            for (Integer checkgroupId : checkgroupIds) {
                baseMapper.addSetmealAndChenkGroupInfo(gid, checkgroupId);
            }
        }
        // 图片提交成功移除Redis中set集合里图片信息
        // setmealDTO.getImg()获取图片名
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET, setmealDTO.getImg());
        return flag;
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Page<Setmeal> page = null;
        //构建条件
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper();
        //查询有效数据
        queryWrapper.eq("is_delete", 0);
        if (StringUtils.isBlank(queryPageBean.getQueryString())) {
            //无条件查询
            page= page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }else {
            //有条件查询
            queryWrapper.like("name", queryPageBean.getQueryString());
            page= page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }


    //任务调度删除阿里云上的垃圾图片
    @Override
    public void xxiJobClearOssImg() {
        //清理 oss垃圾图片 该方法由xxl-job去调用
        //获取Redis中set集合里所有图片信息
        Set<String> members = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        //如果Redis中set集合中有图片
        if (members.size() != 0) {
            //遍历Redis中set集合中的图片名
            for (String uuidfilename : members) {
                //判断在Redis中value集合是否也存在相同的图片名
                boolean existsKey = RedisUtil.existsKey(RedisConstant.SINGLE_PIC + uuidfilename);
                //存在说明还没到超时时间，Redis中value集合还没有自动删除，图片还有效
                if (existsKey == false) {
                    //不存在说明超时，图片失效，那么删除oss中的图片,
                    // 并且一并删除Redis中set集合的失效图片
                    AliyunUtils.deleteFile(uuidfilename);
                    System.out.println("----阿里云删除垃圾图片一次----");
                    //set 集合垃圾图片删除
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET, uuidfilename);
                }
            }
        }
    }

    //移动端查询套餐详情
    @Override
    public SetmealVO findSetmealDetailById(int id) {
        //根据套餐id 查询套餐
        SetmealVO setmealVO = baseMapper.findSetmealById(id);
        //根据套餐id 查询检查项
        List<CheckGroupVO> checkGroupVOList = baseMapper.findCheckGroupInfoBySetmealId(id);
        //根据检查项id 查询 检查项信息
        for (CheckGroupVO checkGroupVO : checkGroupVOList) {
            List<CheckItem> checkItemList = baseMapper.findCheckitemIdsByCheckGroupId(checkGroupVO.getId());
            //将检查项添加到检查组中
            checkGroupVO.setCheckItemList(checkItemList);
        }
        //将检查组添加到套餐中
        setmealVO.setCheckGroupList(checkGroupVOList);
        //System.out.println("添加成功");
        return setmealVO;
    }
}
